package cn.jx.asc.controller;

import cn.jx.asc.common.enums.ResultStatus;
import cn.jx.asc.common.response.Result;
import cn.jx.asc.common.response.ResultUtils;
import cn.jx.asc.common.utils.DataBasesManageUtils;
import cn.jx.asc.common.utils.ExcelUtil;
import cn.jx.asc.pojo.bo.ExportSheetBO;
import cn.jx.asc.pojo.vo.MountainVO;
import cn.jx.asc.pojo.vo.PipeVO;
import cn.jx.asc.service.MountainService;
import cn.jx.asc.service.PipeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 数据管理
 *
 * @author lhg
 */

@Api(value = "数据管理",tags ={"数据管理的相关接口"} )
@RestController
@RequestMapping("/dataManage")
@Slf4j
public class DataManageController {

    @Autowired
    private PipeService pipeService;

    @Autowired
    private MountainService mountainService;

    @RequiresRoles(value = {"ADMIN","DATAMAN"},logical = Logical.OR)
    @ApiOperation(value = "数据库备份", notes = "数据库备份", httpMethod = "POST")
    @PostMapping("/dbBackup")
    public Result dbBackup( @RequestParam String fileName){
        return  DataBasesManageUtils.backup(fileName);
    }

    @RequiresRoles(value = {"ADMIN","DATAMAN"},logical = Logical.OR)
    @ApiOperation(value = "数据库恢复", notes = "数据库恢复", httpMethod = "POST")
    @PostMapping("/dbRecover")
    public Result dbRecover(){

        final String s = DataBasesManageUtils.recover("/workspaces/test/test.sql","42.194.128.233","test","root","root");

        return ResultUtils.resultWithoutData(ResultStatus.SUCCESS,s);
    }

    @ApiOperation(value = "导出注液管7天数据", notes = "导出注液管7天数据", httpMethod = "GET")
    @GetMapping("/exportPipeData7day")
    public Result exportPipeData7day(String sheetName) {
        if (sheetName == null) {
            sheetName = "注液管七天数据";
        }
        final List<PipeVO> list = null;
        ExcelUtil<PipeVO> util = new ExcelUtil<>(PipeVO.class);
        final Date sDate = new Date();
        final Date eData = new Date(-1000 * 60 * 60 * 24 * 7);

        final List<PipeVO> vos = pipeService.queryPipeDataByTime(eData, sDate);

        return util.exportExcel(vos, sheetName);
    }

    @ApiOperation(value = "导出山体7天数据", notes = "导出山体7天数据", httpMethod = "GET")
    @GetMapping("/exportMountainData7day")
    public Result exportMountainData7day(String sheetName) {
        if (sheetName == null) {
            sheetName = "矿山七天数据";
        }
        List<MountainVO> list = null;
        ExcelUtil<MountainVO> util = new ExcelUtil<>(MountainVO.class);
        final Date sDate = new Date();
        final Date eData = new Date(sDate.getTime()-1000 * 60 * 60 * 24 * 7);
        list = mountainService.queryMountainDataByTime(eData, sDate);

        return util.exportExcel(list, sheetName);
    }

    @ApiOperation(value = "导出注液管时间段数据", notes = "导出注液管时间段数据", httpMethod = "POST")
    @PostMapping("/exportPipeDataByTime")
    public Result exportPipeDataByTime(ExportSheetBO exportSheetBO) throws Exception {

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        if (exportSheetBO.getSheetName() == null) {
            exportSheetBO.setSheetName("注液管数据");
        }
        ExcelUtil<PipeVO> util = new ExcelUtil<>(PipeVO.class);

        final List<PipeVO> vos = pipeService.queryPipeDataByTime(
                dateFormat.parse(exportSheetBO.getStartTime()), dateFormat.parse(exportSheetBO.getEndTime()));

        return util.exportExcel(vos, exportSheetBO.getSheetName());
    }

    @ApiOperation(value = "导出矿山时间段数据", notes = "导出矿山时间段数据", httpMethod = "POST")
    @PostMapping("/exportMountainDataByTime")
    public Result exportMountainDataByTime(ExportSheetBO exportSheetBO) throws Exception {

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        if (exportSheetBO.getSheetName() == null) {
            exportSheetBO.setSheetName("注液管数据");
        }
        ExcelUtil<MountainVO> util = new ExcelUtil<>(MountainVO.class);

        final List<MountainVO> vos = mountainService.queryMountainDataByTime(
                dateFormat.parse(exportSheetBO.getStartTime()), dateFormat.parse(exportSheetBO.getEndTime()));
        return util.exportExcel(vos, exportSheetBO.getSheetName());
    }

}
